local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
----------------------------------------------------------------------------------
local function OnHit(inst, owner, target)
    if inst.__attack_task then
        inst.__attack_task:Cancel()
    end
    if inst:HasTag("OnHit_end") then
        return
    end
    inst:AddTag("OnHit_end")
    inst.AnimState:PlayAnimation("win")
    inst.SoundEmitter:KillSound("active_loop")
    inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/win")
    inst.components.projectile:Stop()
    inst:ListenForEvent("animover",function()
        if not inst:IsValid() then
            return
        end
        SpawnPrefab("carnival_confetti_fx").Transform:SetPosition(inst.Transform:GetWorldPosition())
        if inst.__target_fn then
            inst.__target_fn(target)
            inst.__target_fn = nil
        end
        inst:Remove()
    end)
end

local function killer_fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
    inst.entity:AddLight()

    inst.entity:AddDynamicShadow()
    inst.DynamicShadow:SetSize(.8, 0.8)

    
    inst.Light:SetIntensity(.7)
    inst.Light:SetRadius(0.5)
    inst.Light:SetFalloff(.65)
    inst.Light:SetColour(255 / 255, 255 / 255, 236 / 255)
    inst.Light:Enable(false)

    ---------------------------------------------------------------
    if TheWorld.state.isday then
        inst.Light:Enable(false)
    elseif TheWorld.state.isnight then
        inst.Light:Enable(true)
    else
        inst.Light:SetIntensity(.35)
        inst.Light:Enable(true)
    end



    MakeInventoryPhysics(inst)
    RemovePhysicsColliders(inst)

    inst.Transform:SetSixFaced()

    inst.AnimState:SetBank("carnivalgame_herding_chick")
    inst.AnimState:SetBuild("carnivalgame_herding_chick")
    inst.AnimState:PlayAnimation("run_loop",true)
    -- inst.AnimState:PlayAnimation("run_pre")
    -- inst.AnimState:PushAnimation("run_loop",true)

    -- inst:AddTag("projectile")
    inst:AddTag("FX")
    inst:AddTag("INLIMBO")

    inst.entity:SetPristine()

    inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/LP", "active_loop") -- this will be on until it dies,

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)

    inst:AddComponent("projectile")
    inst.components.projectile:SetSpeed(10)     -- wilson 6
    inst.components.projectile:SetHoming(false)
    inst.components.projectile:SetHitDist(2)
    inst.components.projectile:SetOnHitFn(OnHit)
    -- inst.components.projectile:SetOnMissFn(inst.Remove)
    -- inst.components.projectile:SetOnThrownFn(OnThrown)
    inst:DoTaskInTime(0,function()
        if inst.__target == nil or inst.__owner == nil or inst.__target_fn == nil then
            inst:Remove()
        end
    end)


    inst:ListenForEvent("Set",function(inst,_table)
        -- _table = {
        --     pt = pt, 
        --     target = target,
        --     owner = tree,
        --     target_fn = func (target)
        -- }
        if _table == nil then
            return
        end
        if _table.pt == nil or _table.target == nil or _table.owner == nil then
            return
        end
        inst.__target = _table.target
        inst.__owner = _table.owner
        inst.__target_fn = _table.target_fn
        inst.Transform:SetPosition(_table.pt.x,_table.pt.y,_table.pt.z)  
        if _table.speed then
            inst.components.projectile:SetSpeed(_table.speed)
        end
    end)

    inst.__attack_task = inst:DoPeriodicTask(0.3,function()
        local flg,err = pcall(function()
            if inst.__target and inst.__target:IsValid() then
                inst.components.projectile:Throw(inst.__owner,inst.__target)
            else
                ---------------------------------------------------------------------------------------------------
                --- 目标被其他人截杀的时候
                if inst.__attack_task then
                    inst.__attack_task:Cancel()
                end
                inst.AnimState:PlayAnimation("win")
                inst.SoundEmitter:KillSound("active_loop")
                inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/win")
                inst.components.projectile:Stop()
                inst:ListenForEvent("animover",function()
                    SpawnPrefab("carnival_confetti_fx").Transform:SetPosition(inst.Transform:GetWorldPosition())
                    inst:Remove()                    
                end)
                ---------------------------------------------------------------------------------------------------
            end
        end)
        if flg == false then
            inst:Remove()

        end
    end)

    inst:DoTaskInTime(15,inst.Remove)   ----------- 避免目标被杀导致飞太远

    return inst
end
----------------------------------------------------------------------------------

local function item_fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    inst.entity:AddDynamicShadow()
    inst.DynamicShadow:SetSize(.8, 0.8)
    
    inst.Transform:SetSixFaced()

    inst.AnimState:SetBank("carnivalgame_herding_chick")
    inst.AnimState:SetBuild("carnivalgame_herding_chick")
    inst.AnimState:PlayAnimation("idle",true)
    -- inst.AnimState:PlayAnimation("run_pre")
    -- inst.AnimState:PushAnimation("run_loop",true)
    MakeInventoryPhysics(inst)
    -- inst:AddTag("projectile")
    inst:AddTag("FX")
    inst:AddTag("INLIMBO")

    inst.entity:SetPristine()
    inst.SoundEmitter:PlaySound("summerevent/carnival_games/herding_station/chicks/LP", "active_loop") -- this will be on until it dies,

    if not TheWorld.ismastersim then
        return inst
    end

    
    inst:DoTaskInTime(0,function()
        if inst.__cmd_table == nil then
            inst:Remove()
        end
    end)


    inst:ListenForEvent("Set",function(inst,_table)
        -- _table = {
        --     target = target,
        --     owner = owner,
        --     target_fn = func (target)
        -- }
        if _table == nil then
            inst:Remove()
            return
        end
        
        inst.__cmd_table = _table
    end)

    inst:DoTaskInTime(1,function()
        inst.__cmd_table.pt = Vector3( inst.Transform:GetWorldPosition() )
        SpawnPrefab("npc_item_clockwork_egg_killer"):PushEvent("Set",inst.__cmd_table)
        inst:Remove()
    end)

    return inst
end



----------------------------------------------------------------------------------
return Prefab("npc_item_clockwork_egg_killer", killer_fn,assets),
Prefab("npc_item_clockwork_egg_item", item_fn,assets)